package com.kmxd.ams.controller.license;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.kmxd.ams.client.license.IConfLicenseSvc;
import com.kmxd.ams.client.license.dto.ConfLicenseEditDTO;
import com.kmxd.ams.core.license.constant.LicenseConst;
import com.kmxd.ams.core.license.verify.config.LicenseVerifyProperties;
import com.kmxd.ams.core.license.verify.model.LicenseAuthInfo;
import com.kmxd.ams.core.license.verify.model.LicenseVerifyManager;
import com.kmxd.ams.core.license.verify.service.LicenseVerifyService;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import com.kmxd.ams.core.util.AppInfoUtil;
import com.kmxd.ams.infra.license.entity.ConfLicense;
import com.kmxd.ams.license.core.model.LicenseResult;
import de.schlichtherle.license.LicenseContent;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.Date;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 授权信息表 前端控制器
 *
 * @author ZuoLG
 * @since 2023-05-12
 */
@Slf4j
@RestController
@Tag(name = "授权信息表")
@RequestMapping("/license/confLicense")
@AllArgsConstructor
public class ConfLicenseCtl {

  private final LicenseVerifyService licenseVerifyService;
  private IConfLicenseSvc confLicenseService;

  private final AppInfoUtil appInfoUtil;

  /** 授权 */
  @Operation(summary = "授权")
  @PostMapping("/licenseAuth")
  @AutoLog(module = "授权信息表", title = "授权")
  public R<Void> licenseAuth(@Validated @RequestBody ConfLicenseEditDTO req) throws Exception {
    confLicenseService.licenseAuth(req);
    return R.success();
  }

  /** 卸载授权 */
  @Operation(summary = "卸载授权")
  @PostMapping("/uninstallAuth")
  @AutoLog(module = "授权信息表", title = "卸载授权")
  public R<Void> uninstallAuth() throws Exception {
    confLicenseService.uninstallAuth();
    return R.success();
  }

  /** 查看 */
  @Operation(summary = "查看")
  @GetMapping("/licenseAuthInfo")
  @AutoLog(module = "授权信息表", title = "查看")
  public R<LicenseAuthInfo> licenseAuthInfo() {
    ConfLicense confLicense = confLicenseService.getLicenseInfo();
    LicenseVerifyProperties properties = new LicenseVerifyProperties();
    if (ObjectUtil.isNotEmpty(confLicense)) {
      properties.setLicenseCode(confLicense.getRegCode());
    }
    LicenseVerifyManager licenseVerifyManager = new LicenseVerifyManager();
    LicenseResult verifyResult = licenseVerifyManager.verify(properties.getVerifyParam());
    LicenseAuthInfo authInfo = licenseVerifyService.licenseAuthInfo();
    if (authInfo != null) {
      Map<String, Object> params = authInfo.getParams();
      if (ObjectUtil.isNotEmpty(params) && !params.isEmpty()) {
        String fondsNum = MapUtil.getStr(params, LicenseConst.FONDS_NUM);
        authInfo.setFondsNum(fondsNum);
      } else {
        authInfo.setFondsNum(
                ObjectUtil.isEmpty(confLicense) ? null : confLicense.getCompanyNum().toString());
        authInfo.setBeginTime(ObjectUtil.isEmpty(confLicense) ? null : confLicense.getRegTime());
        authInfo.setAfterTime(ObjectUtil.isEmpty(confLicense) ? null : confLicense.getPeriod());
      }
    }
    // 优先获取使用证书时间
    if (BooleanUtil.isTrue(verifyResult.getResult())) {
      LicenseContent licenseContent = verifyResult.getContent();
      Date startTime = licenseContent.getNotBefore();
      Date endTime = licenseContent.getNotAfter();

      authInfo.setBeginTime(startTime);
      authInfo.setAfterTime(endTime);
    }

    authInfo.setAuthFlag(
            verifyResult.getResult()
                    && ObjectUtil.equal(1, ObjectUtil.isEmpty(confLicense) ? 0 : confLicense.getRegFlag()));

    authInfo.setVersion(appInfoUtil.getVersion());
    authInfo.setBuildTime(appInfoUtil.getBuildTime());
    return R.success(authInfo);
  }
}
